#
# @lc app=leetcode.cn id=892 lang=python3
#
# [892] 三维形体的表面积
#
"""
思路：依次统计与周围重合的面
"""

# @lc code=start
import numpy as np
class Solution:
    def surfaceArea(self, grid: List[List[int]]) -> int:
        length = len(grid)
        grid_array = np.array(grid)
        grid_array = np.pad(grid_array, (1, 1), 'constant')
        sum = 0
        for i in range(1, length+1):
            for j in range(1, length+1):
                temp = grid_array[i, j]
                if temp:
                    # 利用减法求解总面积
                    sum += temp * 4 + 2 - min(temp, grid_array[i, j-1]) - min(temp, grid_array[i, j+1]) - min(temp, grid_array[i-1, j]) - min(temp, grid_array[i+1, j])
        return int(sum)


# @lc code=end

